<?

class Qi_Db_Def_CriarTabela
{
	public $tabela = "";
	public $colunas = array();

	public function __construct($tabela)
	{
		$this->tabela = $tabela;
	}

	public function id()
	{
		$this->colunas["id"] = "INT AUTO_INCREMENT PRIMARY KEY";
		return $this;
	}

	public function coluna($nome, $conf)
	{
		$this->colunas[$nome] = $conf;
		return $this;
	}

	public function string($nomes)
	{
		$nomes = is_array($nomes) ? $nomes : func_get_args();
		foreach($nomes as $nome):
			$this->coluna($nome, "VARCHAR(255)");
		endforeach;
		return $this;
	}

	public function int($nomes)
	{
		$nomes = is_array($nomes) ? $nomes : func_get_args();
		foreach($nomes as $nome):
			$this->coluna($nome, "INT");
		endforeach;
		return $this;
	}

	public function sql()
	{
		$sql = "CREATE TABLE $this->tabela (\n";
		$parts = array();
		foreach($this->colunas as $nome => $conf):
			$parts[] = "$nome $conf";
		endforeach;
		$sql .= implode(",\n\t", $parts);
		$sql .= ")";
		return $sql;
	}

	public function exec()
	{
		Qi_Db::query($this->sql());
	}

	public function end()
	{
		return $this->exec();
	}
}

?>